package cn.com.hhrcw.job;

import cn.com.hhrcw.entity.OpInterviewContact;
import cn.com.hhrcw.entity.OpInterviewHistory;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.message.MechanismService;
import cn.com.hhrcw.service.IOpInterviewContactService;
import cn.com.hhrcw.service.IOpInterviewHistoryService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.sms.service.ISysSmsTemplateService;
import cn.com.hhrcw.utils.*;
import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: stdss-boot-parent
 * @description:
 * @author: gjf
 * @create: 2020-11-28 16:09
 **/
@Slf4j
@Component("DepartJobTask")
public class DepartJobTask implements Job {

    @Autowired
    private IOpInterviewHistoryService opInterviewHistoryService;

    @Autowired
    private MechanismService mechanismService;

    @Autowired
    private ISysSmsTemplateService service;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOpInterviewContactService opInterviewContactService;

    @Autowired
    private ISysSmsTemplateService templateService;

    @Autowired
    private NewsUtil newsUtil;

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private EmailSendUtil emailSendUtil;

    @Autowired
    private IPiInfoService piInfoService;

    @Override
    public void execute(JobExecutionContext context) {
        String key1 = CommonConstant.COMMON_SEND_KEY;

        try {
            if (RedisLockUtils.tryLock(key1, 1, TimeUnit.MINUTES)) {
                log.info("即将开始面试通知定时任务");
                List<OpInterviewHistory> historyList = opInterviewHistoryService.getSoonInterview();
                if (CollUtil.isNotEmpty(historyList)) {
                    for (OpInterviewHistory e : historyList) {
                        String id = e.getId();
                        Date time = e.getTime();
                        long time1 = DateUtil.getTime(cn.hutool.core.date.DateUtil.date(), time);
                        Boolean key = redisTemplate.hasKey(CommonConstant.COMMON_SEND_KEY + id);
                        if (!key) {
                            redisTemplate.opsForValue().set(CommonConstant.COMMON_SEND_KEY + id, e, time1, TimeUnit.SECONDS);
                            String eUserId = e.getUserId();
                            PiInfo piInfo = piInfoService.lambdaQuery().eq(PiInfo::getUserId, eUserId).one();
                            newsUtil.creatNews(eUserId, "admin", 0, 1, id, "12");
                            Map<String, String> map = emailUtil.creatEmail(eUserId, "admin", null, null, id, "12");
                            String title = map.get("tittle");
                            String content = map.get("content");
                            //短信通知不合适
                            templateService.sendSms(new String[]{piInfo.getPhone()}, "【" + title + "】" + content);
                            Map<String, String> map2 = emailSendUtil.creatEmail(eUserId, "admin", null, null, id, "12");
                            String title2 = map2.get("tittle");
                            String content2 = map2.get("content");

                            service.sendEmail(content2, title2, (File) null, piInfo.getEmail());
                            List<OpInterviewContact> list = opInterviewContactService.lambdaQuery().eq(OpInterviewContact::getViewId, id)
                                .eq(OpInterviewContact::getDelFlag, false).list();
                            if (CollUtil.isNotEmpty(list)) {
                                list.forEach(contact -> {
                                    String userId = contact.getContactId();
                                    PiInfo piInfo1 = piInfoService.lambdaQuery().eq(PiInfo::getUserId, userId).one();
                                    newsUtil.creatNews(userId, eUserId, 1, 0, id, "13");
                                    Map<String, String> map1 = emailUtil.creatEmail(userId, "admin", null, null, id, "13");
                                    String title1 = map1.get("tittle");
                                    String content1 = map1.get("content");
                                    //短信通知不合适
                                    templateService.sendSms(new String[]{piInfo1.getPhone()}, "【" + title1 + "】" + content1);

                                    Map<String, String> map3 = emailSendUtil.creatEmail(userId, "admin", null, null, id, "13");
                                    String title3 = map3.get("tittle");
                                    String content3 = map3.get("content");

                                    service.sendEmail(content3, title3, (File) null, piInfo1.getEmail());
                                });
                            }

                        }

                    }
                }
            }

        } finally {
            RedisLockUtils.unLock(key1);
        }

    }

}
